<?php

defined('PRESSLESS_IS_RUNNING') or die("This script is not meant to be run stand-alone.");

// Make development the default; accidentally landing in production wouldn't be a particularly good idea.
if ( !getenv('PRESSLESS_ENV') ) putenv('PRESSLESS_ENV=development');

// After this, never use the environment variable anywhere else. Use this constant instead.
define('PRESSLESS_ENV', getenv('PRESSLESS_ENV'));

define('PRESSLESS_ROOT_DIR',     realpath(dirname(__FILE__) . '/..')  ); // Where the application is living
define('PRESSLESS_CACHE_DIR',    PRESSLESS_ROOT_DIR . '/cache');         // Where caches are kept
define('PRESSLESS_CUSTOM_DIR',   PRESSLESS_ROOT_DIR . '/custom');        // Everything application-specific
define('PRESSLESS_ADDONS_DIR',   PRESSLESS_ROOT_DIR . '/addons');        // Where addons are installed
define('PRESSLESS_PRODUCT_DIR',  PRESSLESS_ROOT_DIR . '/factory');       // Where Pressless is installed

require_once(PRESSLESS_PRODUCT_DIR . '/libs/pressless-functions.php');

set_error_handler('pressless_error_handler', E_USER_ERROR | (PRESSLESS_ENV == 'development' ? E_WARNING | E_USER_WARNING : 0));
set_exception_handler('pressless_exception_handler');

// Rebuild the cache during installation or in development
if ( PRESSLESS_ENV == 'development' or PRESSLESS_ENV == 'install' or PRESSLESS_ENV == 'deploy' )
  require_once(PRESSLESS_PRODUCT_DIR . '/cache.php');

require_once(PRESSLESS_CACHE_DIR.'/addons.php');
require_once(PRESSLESS_CACHE_DIR.'/actions.php');

//
// Initialization of the localization features
//

if ( !defined('PRESSLESS_GETTEXT') ) {
  // Does this PHP installation have the gettext extension?
  if ( function_exists('gettext') ) {
    define('PRESSLESS_GETTEXT', 'EXT');
  }
  // Or, do we have to use the slower pure PHP gettext implementation?
  else {
    define('PRESSLESS_GETTEXT', 'PHP');
    require_once(PRESSLESS_PRODUCT_DIR . '/libs/gettext.inc');
  }
}

require_once(PRESSLESS_PRODUCT_DIR . '/libs/l10n.php');

//
// Addons initialization
//

$pressless_addons_installed = array();
$pressless_addons_enabled = array();
$pressless_addons_initialized = array();

if (  file_exists(PRESSLESS_CUSTOM_DIR . '/addons-enabled.php')  )
  include(PRESSLESS_CUSTOM_DIR . '/addons-enabled.php');

if (  file_exists(PRESSLESS_CUSTOM_DIR . '/' . PRESSLESS_ENV . '/addons-enabled.php')  )
  include(PRESSLESS_CUSTOM_DIR . '/' . PRESSLESS_ENV . '/addons-enabled.php');

ini_set('include_path', pressless_include_path());

foreach ($pressless_addons_enabled as $addon_being_enabled)
{
  require_once(PRESSLESS_ADDONS_DIR . "/$addon_being_enabled/initialize.php");
  $pressless_addons_initialized[] = $addon_being_enabled;
}
unset($addon_being_enabled);

//
// The addons have been initialized; now we can initialize the other custom code (which may depend on addons)
//

if (  file_exists(PRESSLESS_CUSTOM_DIR . '/initialize.php')  )
  include(PRESSLESS_CUSTOM_DIR . '/initialize.php');

if (  file_exists(PRESSLESS_CUSTOM_DIR . '/' . PRESSLESS_ENV . '/initialize.php')  )
  include(PRESSLESS_CUSTOM_DIR . '/' . PRESSLESS_ENV . '/initialize.php');

//
// Defaults
//


//
// That's it. We'll probably return to the dispatcher now.
//

?>
